package org.finesys.common.security.authentication.handler;

import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 成功登出时调用
 */
public class SsoLogoutSuccessHandler implements LogoutSuccessHandler {

    private final static String REDIRECT_URL = "redirect_url";

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        if (response == null) {
            return;
        }
        //获取请求参数中是否包含 回调地址
        String redirectUrl = request.getParameter(REDIRECT_URL);
        if (StringUtils.hasText(redirectUrl)) {
            response.sendRedirect(redirectUrl);
            return;
        }
        //头部地址
        String refer = request.getHeader(HttpHeaders.REFERER);
        if (StringUtils.hasText(refer)) {
            response.sendRedirect(refer);
        }
    }
}
